import { proxy, ref } from "valtio";
import demo from "@/json/demo.json";
import { currentType, treeData } from "@/type";

const currentHandleState = proxy({
  domTreeData: ref(demo) as unknown as treeData.treeDataType,
  currentAddComponentLocation: "" as
    | "start"
    | ""
    | { location: string; key?: string },
  currentAddComponentIsLeft: false,
  currentHandleComponentLocation: "",
});

const currentHandleActions = {
  setDomTreeData: (val: treeData.treeDataType) => {
    currentHandleState.domTreeData = val;
  },
  setCurrentAddComponentLocation: (
    val: currentType.currentAddComponentLocationType
  ) => {
    if (val) {
      currentHandleState.currentAddComponentLocation = val;
    }
  },
  clearCurrentAddComponentLocation: () => {
    console.log("clearCurrentAddComponentLocation", new Date().getTime());

    currentHandleState.currentAddComponentLocation = "";
  },
  setCurrentAddComponentIsLeft: (val: boolean) => {
    currentHandleState.currentAddComponentIsLeft = val;
  },
  setCurrentHandleComponentLocation: (val: string) => {
    currentHandleState.currentHandleComponentLocation = val;
  },
};

export { currentHandleState, currentHandleActions };
